package com.mz.ozy.activity.assets;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.BalanceDetailAdapter;
import com.mz.ozy.adapter.RedeRecordAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.BalanceDetailBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.BalanceDetailPresenter;
import com.mz.ozy.mvp.view.BalanceDetailContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 余额明细
 */
public class BalanceDetailsActivity extends BaseActivity<BalanceDetailContract.View, BalanceDetailPresenter> implements BalanceDetailContract.View {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    private BalanceDetailAdapter mBalanceDetailAdapter;
    private RedeRecordAdapter mHomeAdapter;
    private int pageNo = 1;
    private int tag = 0;
    private List<BalanceDetailBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();
    private int mType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance_details);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        initTitle();
        mType = getIntent().getIntExtra("type", 0);
        switch (mType) {
            case 0:
                tvCenterTitle.setText(R.string.assets_balance_wallet_detail);
                break;
            case 1:
                tvCenterTitle.setText(R.string.assets_bonus_wallet_detail);
                break;
            case 2:
                tvCenterTitle.setText(R.string.assets_eth_detail);
                break;
            case 4:
                tvCenterTitle.setText(R.string.rede_record);
                break;
        }
        tvTitle.setVisibility(View.GONE);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mBalanceDetailAdapter = new BalanceDetailAdapter(this, mBeanList);
        mHomeAdapter = new RedeRecordAdapter(mBeanList);
        if (mType != 4) {
            recyclerview.setAdapter(mBalanceDetailAdapter);
        } else {
            recyclerview.setAdapter(mHomeAdapter);
        }
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getBalanceDetail(BalanceDetailsActivity.this, mType, String.valueOf(pageNo));
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getBalanceDetail(BalanceDetailsActivity.this, mType, String.valueOf(pageNo));
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String event) {
        if (event.equals(Constants.EventCode.REFRESH_DETAIL_LIST)) {
            smartRefreshLayout.autoRefresh();
        }

    }

    @Override
    public void getBalanceDetailSuccess(BalanceDetailBean bean) {
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }

        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        if (mType != 4) {
            mBalanceDetailAdapter.notifyDataSetChanged();
        } else {
            mHomeAdapter.notifyDataSetChanged();
        }
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
